package framework;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;

public class BasicActionGetter implements ActionGetter {
	
	private Properties prop;
	
	private final Hashtable<String, Controls> controls = new Hashtable<>();
	
	public BasicActionGetter() {
	    this(null, null);
	}

	public BasicActionGetter(BasicFighter fighter1, BasicFighter fighter2) {
		prop = new Properties();
		
		try {
			File file = new File("resources/controls.properties");
			FileInputStream in = new FileInputStream(file);
			prop.load(in);
			loadKeys();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public Controls getAction(int controller, String input) {
		return this.controls.get(formatInput(controller, input));
	}
	
	public void loadKeys() {
		controls.put(prop.getProperty("controls.p1.a"), Controls.A_P1);
		controls.put(prop.getProperty("controls.p1.up"), Controls.UP_P1);
		controls.put(prop.getProperty("controls.p1.b"), Controls.B_P1);
		controls.put(prop.getProperty("controls.p1.x"), Controls.X_P1);
		controls.put(prop.getProperty("controls.p1.y"), Controls.Y_P1);
		controls.put(prop.getProperty("controls.p1.start"), Controls.START_P1);
		controls.put(prop.getProperty("controls.p1.parry"), Controls.PARRY_P1);
		controls.put(prop.getProperty("controls.p1.down"), Controls.DOWN_P1);
		controls.put(prop.getProperty("controls.p1.left"), Controls.LEFT_P1);
		controls.put(prop.getProperty("controls.p1.right"), Controls.RIGHT_P1);
		controls.put(prop.getProperty("controls.p2.a"), Controls.A_P2);
		controls.put(prop.getProperty("controls.p2.b"), Controls.B_P2);
		controls.put(prop.getProperty("controls.p2.x"), Controls.X_P2);
		controls.put(prop.getProperty("controls.p2.y"), Controls.Y_P2);
		controls.put(prop.getProperty("controls.p2.start"), Controls.START_P2);
		controls.put(prop.getProperty("controls.p2.parry"), Controls.PARRY_P2);
		controls.put(prop.getProperty("controls.p2.up"), Controls.UP_P2);
		controls.put(prop.getProperty("controls.p2.down"), Controls.DOWN_P2);
		controls.put(prop.getProperty("controls.p2.left"), Controls.LEFT_P2);
		controls.put(prop.getProperty("controls.p2.right"), Controls.RIGHT_P2);
		controls.put(prop.getProperty("controls.p1.teleport"), Controls.TELEPORT_P1);
		controls.put(prop.getProperty("controls.p2.teleport"), Controls.TELEPORT_P2);
	}
	
	private String formatInput(int controller, String button) {
		String device = "";
		if (controller == -1) {
			device = "k";
		} else if(controller == 0) {
			device = "c1";
		} else {
			device = "c2";
		}
		return device + C.CONTROL_SEPARATOR + button;
	}
	
	public Properties getProperties() {
		return prop;
	}
}
